package com.luoshengsha.client;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.remoting.RemoteConnectFailureException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.luoshengsha.bean.Order;

@RestController
public class OrderClient {
	@Autowired
    private OrderService orderService;

	@GetMapping("/place_order")
    public void placeOrder() {
        try {
			System.out.println("-- placing orders --");
			orderService.placeOrder("ABC Tablet", 2);
			orderService.placeOrder("XYZ Desktop", 3);
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof RemoteConnectFailureException) {
				System.out.println("请求远程RMI服务器("+ PropertiesConfig.rmi_server +")失败");
			} else if(e instanceof RemoteAccessException) {
				System.out.println("请求远程RMI服务器地址格式有误，请重新设置");
			}
		}
    }

	@RequestMapping("/list_orders")
    public List<Order> listOrders() {
        try {
			System.out.println("-- getting order list from service --");
			List<Order> orderList = orderService.getOrderList();
			System.out.println(orderList);
			return orderList;
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof RemoteConnectFailureException) {
				System.out.println("请求远程RMI服务器("+ PropertiesConfig.rmi_server +")失败");
			} else if(e instanceof RemoteAccessException) {
				System.out.println("请求远程RMI服务器地址格式有误，请重新设置");
			}
			return null;
		}
    }
}
